package com.seven.monior.AppInfo;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.seven.monior.AppDetail;
import com.seven.monior.R;

public class AppItemAdapter extends BaseAdapter {

    private Context context;
    private List<AppInfo> items;
    private LayoutInflater mInflater;
    
    public AppItemAdapter(Context ctx) {
    	context=ctx;
    	mInflater = LayoutInflater.from(ctx);
        items = new ArrayList<AppInfo>();
    }
    
    public void AddData(ArrayList<AppInfo> mList){
    	items=mList;
    }

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder;  
		if (convertView == null) {
              holder=new ViewHolder(); 
              convertView = mInflater.inflate(R.layout.apps_list_item, null);
              holder.appIcon = (ImageView)convertView.findViewById(R.id.appIcon);
              holder.appName = (TextView)convertView.findViewById(R.id.appName);
              holder.appSize = (TextView)convertView.findViewById(R.id.appSize);
              holder.appVersion = (TextView)convertView.findViewById(R.id.appVersion);
              holder.appIsRunning = (ProgressBar)convertView.findViewById(R.id.appIsRunning);
              holder.runningProcess = (ListView)convertView.findViewById(R.id.listView_processes);
              convertView.setTag(holder);
              
		}else {       
			holder = (ViewHolder)convertView.getTag();
		}
		holder.appIcon.setImageDrawable(items.get(position).getIcon());
		holder.appName.setText((String)items.get(position).getAppName());
		holder.appSize.setText((String)items.get(position).getPName());
		holder.appVersion.setText((String)items.get(position).getVersionName());
		

		
		if(!items.get(position).getIsRunning()){
			holder.appIsRunning.setVisibility(View.GONE);
			holder.runningProcess.setVisibility(View.GONE);

		}
		else{
			//holder.appIsRunning.setIndeterminate(false);
			
			//Log.d("SETINTERMINATE", "SETINTERMINATE");
			
			
	        ProcessItemAdapter processItemAdapter = new ProcessItemAdapter(context);
	        
	        ArrayList<RunningAppProcessInfo> list = items.get(position).getRunningAppProcessInfos();
	        if(list!=null && list.size()>0){
	        	processItemAdapter.AddData(list);
	        	holder.runningProcess.setAdapter(processItemAdapter);
	        }
	        
	        holder.runningProcess.setVisibility(View.VISIBLE);
	        
	        holder.appIsRunning.setVisibility(View.VISIBLE);
		}

		           
		return convertView;
	}

}

class ViewHolder{
	public ImageView appIcon;
	public TextView appName;
	public TextView appSize;
	public TextView appVersion;
	public ImageView processView;
	
	public ImageView processIcon;
	public TextView processName;
	public TextView processID;
	public TextView processStatus;
	public TextView processLogs;
	
	public ProgressBar appIsRunning;
	public ListView runningProcess;
}


